
var EpyItemTest = new Class({
    Implements: Options,
    options: {
        'tagid' : ''
    },
    panel: null,
    content: null,
    listData: null,
    result:null,
    resGroupPrice:0,
    resGroupQty:0,
    initialize: function(opts) {
        this.setOptions(opts);

        this.panel = document.id(this.options.tagid);
        if(!this.panel) return;
        this.panel.empty();

        this.createTable();
    },
    createTable: function() {
        this.content = new Element('div#content');
        this.listData = new Element('div');

        this.panel.adopt(
            new Element('div.tl').adopt('div.tr').adopt('div[html=&nbsp;]'),
            this.content.adopt(this.listData),
            new Element('div.bl').adopt('div.br').adopt('div[html=&nbsp;]')
        )

        this.divData = new Element('div');
        this.listData.adopt(
            new Element('b[text=AccwareSKU : ]'),
            new Element('input[type=text][size=13][maxlength=15]'),
            new Element('button[text=TEST]',{
                'events': {
                    'click': this.getItem.bind(this),
                    'focus': function() {this.blur();}
                }
            }),
            this.divData
        );
    },
    getItem: function(evt) {
        evt.stop();

        var sku = this.listData.getElement('input');
        if(sku.get('value') == '') {
            alert('Field AccwareSKU cannot be empty.');
            return;
        }

        this.getItem = new Request({
            url: '/Admin/webstat/httpservice',
            method: 'get',
            onRequest: function() {
                new ShowBox().say('Get Item...');
                new Loader().show();
            },
            onComplete: function(resp) {
                var data = [];
                try {
                    data = eval('(' + resp + ')');
                }
                catch(err) { alert('Error...\n' + err); return; }
                new Loader().hide();

                this.showItem(data);
            }.bind(this)
        }).send('table=epy&ex=getTestItem&sku=' + sku.get('value') + '&nc=' + Date.now());

    },
    showItem: function(data) {

        this.divData.empty();
        this.item = data.item;

        var item = data.item;
        var table;
        if(item.id) {
            // item table
            table = this.itemBody();
            this.divData.adopt(
                new Element('hr'),
                new Element('div[html=<h3>Item Name: ' + this.item.name + '</h3>][style=margin:10px;]'),
                table
            );

            if(item.active) {

                if(item.purchasesend && item.vid == 2) {
                    table = this.purchaseBody();
                    this.divData.adopt(
                        new Element('hr'),
                        new Element('div[html=<h3>Compuvest Purchase:</h3>][style=margin:10px;]'),
                        table
                    );
                }

                if(item.grpname) {
                    table = this.groupBody(item);
                    this.divData.adopt(
                        new Element('hr'),
                        new Element('div[html=<h3>Group: ' + this.item.grpname + '</h3>][style=margin:10px;]'),
                        table
                    );

                    if(item.vactive) {
                        table = this.vendorBody(item);
                        this.divData.adopt(
                            new Element('hr'),
                            new Element('div[html=<h3>Vendor: ' + this.item.vendname + '</h3>][style=margin:10px;]'),
                            table
                        );
                    }
                    else {
                        this.divData.adopt(
                            new Element('hr'),
                            new Element('div[html=<h3>Vendor: <b class="red">Vendor: ' + this.item.vendname + ' Not Active.</b></h3>][style=margin:10px;]')
                        );
                    }
                }
                else {
                    this.divData.adopt(
                        new Element('hr'),
                        new Element('div[html=<h3><b class="red">Do not find such group.</b></h3>][style=margin:10px;]')
                    );
                }
            }
            else {
                this.divData.adopt(
                    new Element('hr'),
                    new Element('div[html=<h3><b class="red">Do not find such item.</b></h3>][style=margin:10px;]')
                );
            }
        }
        else {
            this.divData.adopt(
                new Element('hr'),
                new Element('div[html=<h3>Item <b class="red">Do not have any information.</b></h3>][style=margin:10px;]')
            );
        }

    },
    purchaseBody: function() {

        var tbody = new Element('tbody');
        var table = new Element('table#itmtst').adopt(
            tbody.adopt(
                new Element('tr', { html: '<th>&nbsp;</th><th>Purchase Group Name</th><th>Perchase Name</th><th colspan=2>&nbsp;</th>' }),
                new Element('tr', { html:
                    '<td>&nbsp;</td>' +
                    '<td>' + this.item.purchasegroupname + '</td>' +
                    '<td>' + this.item.purchaseusername + '</td>' +
                    '<td colspan=2>&nbsp;</td>'
                }),
                new Element('tr', { html: '<th>&nbsp;</th><th>Price</th><th>Qty</th><th>Price after</th><th>Qty after</th>' })
            )
        );

        var purchase, qty, pass;
        this.item.advert.each(function(advert) {
            purchase = advert.purchase;

            qty = purchase.stock ? purchase.qty : (purchase.qty - this.item.qty > 0) ? this.item.qty : purchase.qty;
            pass = (qty > 0 && purchase.price > 0);

            purchase.pass = !pass;

            tbody.adopt(new Element('tr' + (pass ? '' : '.hide'), { html:
                '<th class="right' + (pass ? ' bgreen' : ' bgred') + '">' + advert.name + '</th>' +
                '<td>' + formatprice(purchase.price) + '</td>' +
                '<td>' + (purchase.qty || '') + '</td>' +
                '<td ' + (purchase.price > 0 ? '' : 'class="red"') + '>' + formatprice(purchase.price) + '</td>' +
                '<td ' + (qty > 0 ? '' : 'class="red"') + '>' + (qty || '') + '</td>'
            }))
        }, this);
        return table;
    },
    itemBody: function() {
        // Item
        var table = new Element('table#itmtst').adopt(
            new Element('tbody').adopt(
                new Element('tr', { html:
                    '<th>SKU</th><th>Category</th><th>Vendor</th><th>Cost Price</th><th>Sell Price</th><th>Resell Price</th><th>Stock</th>' +
                    '<th>Quantity</th><th>Created</th><th>Updated</th><th>Google Update</th><th>Google Action</th>'
                }),
                new Element('tr' + (this.item.active ? '' : '.hide'), { html:
                    '<td>' + this.item.sku + '</td>' +
                    '<td>' + (this.item.catname ? this.item.catname : '<b class="red">UNDEFINED CATEGORY</b>')  + '</td>' +
                    '<td class="green">' + this.item.vendname + '</td>' +
                    '<td>' + formatprice(this.item.costprice) + '</td>' +
                    '<td>' + formatprice(this.item.sellprice) + '</td>' +
                    '<td>' + formatprice(this.item.hotprice) + '</td>' +
                    '<td>' + (this.item.stock ? 'Y' : 'N') + '</td>' +
                    '<td>' + this.item.qty + '</td>' +
                    '<td>' + this.item.created + '</td>' +
                    '<td>' + this.item.updated + '</td>' +
                    '<td>' + this.item.google_update + '</td>' +
                    '<td>' + this.item.google_action + '</td>'
                })
            )
        );
        return table;
    },
    groupBody: function(item) {

        var activeday = this.getOlderDay();
        var tbody = new Element('tbody');
        var table = new Element('table#itmtst').adopt(
            tbody.adopt(
                new Element('tr', { html:
                    '<th>Older</th><th>Active</th><th>DefQty</th><th>SubQty</th><th>Add Margin</th><th>Add Charge</th><th>Price after</th><th>Qty after</th>'
                }),
                new Element('tr', { html:
                    '<th class="bgreen">' + activeday.day + '</th>' +
                    '<td class="green">' + (this.item.groupactive ? 'Yes' : 'No') + '</td>'
                })
            )
        );

        var group, vendor, price, qty, pass;
        this.item.advert.each(function(advert) {
            group = this.getOlderGroup(advert.name, activeday.day);
            vendor = advert.vendor;

            price = vendor.groupprice = (group.price / 100 * group.margin) + group.price + group.charge;
            qty = vendor.groupqty = (item.stock ? group.defQty : item.qty - group.minQty);
            pass = vendor.grouppass = (advert.purchase.pass && qty > 0 && price > 0);

            tbody.adopt(
                new Element('tr' + (pass ? '' : '.hide'), { html:
                    '<th class="right ' + (pass ? 'bgreen': 'bgred') + '" colspan=2>' + advert.name + '</th>' +
                    '<td>' + (group.defQty || '') + '</td>' +
                    '<td>' + (group.minQty || '') + '</td>' +
                    '<td>' + formatpercent(group.margin) + '</td>' +
                    '<td>' + formatpercent(group.charge) + '</td>' +
                    '<td' + (price > 0 ? '': ' class="red"') + '>' + formatprice(price) + '</td>' +
                    '<td' + (qty > 0 ? '': ' class="red"') + '>' + qty + '</td>'
                })
             )
        }, this);

        return table;
    },
    vendorBody: function(item) {

        var tbody = new Element('tbody');
        var table = new Element('table#itmtst').adopt(
            tbody.adopt(
                new Element('tr', { html:
                    '<th>&nbsp</th><th>Send</th><th>Add Margin</th><th>Add Charge</th><th>Sub Qty</th><th>Price after</th><th>Qty after</th>'
                })
            )
        );

        var vendor, price, qty, pass;
        this.item.advert.each(function(advert) {
            vendor = advert.vendor;

            price = (vendor.groupprice / 100 * vendor.margin) + vendor.groupprice + vendor.charge;
            qty = vendor.groupqty - vendor.qty;

            pass = (vendor.grouppass && vendor.send && qty > 0 && price > 0);

            tbody.adopt(
                new Element('tr' + (pass ? '' : '.hide'), { html:
                    '<th class="right ' + (pass ? 'bgreen': 'bgred') + '">' + advert.name + '</th>' +
                    '<td' + (vendor.send ? '' : ' class="red"') + '>' + (vendor.send ? 'Yes' : 'No') + '</td>' +
                    '<td>' + formatpercent(vendor.margin) + '</td>' +
                    '<td>' + formatprice(vendor.charge) + '</td>' +
                    '<td>' + (vendor.qty || '') + '</td>' +
                    '<td' + (price > 0 ? '' : ' class="red"') + '>' + formatprice(price) + '</td>' +
                    '<td' + (qty > 0 ? '' : ' class="red"') + '>' + qty + '</td>'
                })
            )
        });

        return table;
    },
    getOlderDay: function() {

        var active = this.item.older.filter(function(old) {
            return old.active == 1;
        });
        return active[0];
    },
    getOlderGroup: function(name, day) {

        var group = this.item.advert.filter(function(advert) {
            return advert.name == name;
        });

        var array = group[0].groups.filter(function(group) {
            return group.name == day;
        });
        return array[0];
    }
});
